/**
 * created by 小卷毛, 2019/09/03 0014
 * Copyright (c) 2019, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */

package com.aifeng.base

import android.text.Spanned
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.aifeng.common_ui.base.getColorEx
import com.aifeng.common_ui.base.getDrawableEx
import com.aifeng.common_ui.helper.dp2px
import com.aifeng.common_ui.helper.fromHtmlEx
import com.aifeng.hassan_food.R

/**
 * 文本加载扩展类
 */
fun TextView.fromHtml(text: String) {
    setText(fromHtmlEx(text))
}

fun TextView.setColor(text: String, key: String) {
    setText(
        fromHtmlEx(
            text.replace(
                key,
                "<font color='${context.getColorEx(R.color.colorAccent)}'>$key</font>"
            )
        )
    )
}

fun TextView.setColor(text: String, key: String, color: String) {
    setText(fromHtmlEx(text.replace(key, "<font color='$color'>$key</font>")))
}

fun TextView.setUnicodeText(text: String) {
    if (text.isEmpty()) setText(text)
    else {
        var start = 0
        var end: Int
        val buffer = StringBuilder()
        while (start > -1) {
            end = text.indexOf("\\u", start + 2)
            val charStr = text.substring(start + 2 until if (end == -1) text.length else end)
            val letter = Integer.parseInt(charStr, 16).toChar() // 16进制parse整形字符串
            buffer.append(Character.valueOf(letter).toString())
            start = end
        }
        setText(buffer.toString())
    }
}

fun TextView.setCompoundDrawable(
    @DrawableRes leftDrawable: Int = 0,
    @DrawableRes rightDrawable: Int = 0,
    @DrawableRes topDrawable: Int = 0,
    @DrawableRes bottomDrawable: Int = 0,
    leftSize: Float = 15f,
    rightSize: Float = 15f,
    topSize: Float = 15f,
    bottomSize: Float = 15f
) {
    setCompoundDrawables(
        if (leftDrawable != 0)
            context.getDrawableEx(leftDrawable).apply {
                setBounds(0, 0, dp2px(leftSize), dp2px(leftSize))
            } else null,
        if (topDrawable != 0)
            context.getDrawableEx(topDrawable).apply {
                setBounds(0, 0, dp2px(topSize), dp2px(topSize))
            } else null,
        if (rightDrawable != 0)
            context.getDrawableEx(rightDrawable).apply {
                setBounds(0, 0, dp2px(rightSize), dp2px(rightSize))
            } else null,
        if (bottomDrawable != 0)
            context.getDrawableEx(bottomDrawable).apply {
                setBounds(0, 0, dp2px(bottomSize), dp2px(bottomSize))
            } else null
    )
}

fun setHtmlSmall(text: String, key: String): Spanned =
    fromHtmlEx(text.replace(key, "<small>$key</small>"))

fun setHtmlColor(
    text: String,
    key: String,
    color: String = "#C20D23"
): Spanned =
    fromHtmlEx(text.replace(key, "<font color='$color'>$key</font>"))